package org.monitor.controller.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.Map;
import java.util.Map.Entry;

import org.monitor.config.MonitorConfig;
import org.monitor.ctx.WebEnvCtx;
import org.monitor.http.RestResult;
import org.monitor.kit.FileKit;
import org.monitor.kit.IPKit;
import org.monitor.servlet.controller.Controller;
import org.monitor.servlet.pairs.Pairs;

@Deprecated
public class UploadController extends Controller {
	
	public void index(){
		Pairs pairs = getPairs();
		Map<String, String> fileMap = pairs.getFiles();
		
		for (Entry<String, String> entry : fileMap.entrySet()) {
			String filename = entry.getKey();
			String tmpFilePath = entry.getValue();
			
			File tmpFile = new File(tmpFilePath);
			saveFile(pairs, filename, tmpFile);
		}
		
		renderJson(new RestResult());
	}

	private void saveFile(Pairs paras, String filename, File tmpFile) {
		File storeFile = getStoreFile(paras, filename);
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(tmpFile);
			fos = new FileOutputStream(storeFile);
			
			byte[] buffer = new byte[1024];
			int in = 0;
			while((in = fis.read(buffer)) != -1){
				fos.write(buffer, 0, in);
			}
			
			fos.flush();
			
		} catch (Exception e) {
		}finally {
			FileKit.close(fis);
			FileKit.close(fos);
		}
		
		tmpFile.delete();
	}

	private File getStoreFile(Pairs paras, String filename) {
		File storeFile = null;
		
		String ip = IPKit.getIp(WebEnvCtx.get().getRequest());
		int port = IPKit.getPort(WebEnvCtx.get().getRequest());
		String userId = paras.getPair("userid");
		if(userId == null){
			userId = "anonymous";
		}
		
		String storeDir = MonitorConfig.get().getStoreDir() + File.separator + ip + "_" + port + File.separator + userId + File.separator;
		File store = new File(storeDir);
		if(!store.exists()){
			store.mkdirs();
		}
		
		String storeFilename = getStoreFilename(filename, store);
		storeFile = new File(store.getPath() + File.separator + storeFilename);
		if(storeFile.exists()){
			storeFile.delete();
		}
		
		return storeFile;
	}

	private String getStoreFilename(String filename, File store) {
		String storeFilename = null;
		if(MonitorConfig.get().override()){
			storeFilename = filename;
		}else {
			int index = filename.lastIndexOf(".");
			final String baseName = index != -1 ?filename.substring(0, index) : filename ;
			String suffix = index != -1 ? filename.substring(index + 1) : null;
			
			String[] files = store.list(new FilenameFilter() {
				@Override
				public boolean accept(File dir, String name) {
					return name.startsWith(baseName);
				}
			});
			
			storeFilename = baseName;
			if(files != null && files.length > 0){
				storeFilename = storeFilename + "_" + files.length;
			}
			if(suffix != null){
				storeFilename = storeFilename + "." + suffix;
			}
		}
		return storeFilename;
	}

}
